package com.junbao.message.infrastructure.queue.message;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.junbao.message.domain.enums.ProducerName;
import com.junbao.message.domain.model.NotifyMessage;
import com.junbao.message.infrastructure.core.RequestDto;
import com.junbao.message.infrastructure.core.dispatcher.MessageConfirmDisruptor;
import com.junbao.message.infrastructure.core.dispatcher.MessageDeadDisruptor;
import com.junbao.message.infrastructure.queue.AbstractTask;

@SuppressWarnings("all")
public class ConfirmMessageTask extends AbstractTask {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfirmMessageTask.class);
 
    private final NotifyMessage message;
 
    private MessageConfirmDisruptor messageConfirmDisruptor;
 
    private MessageDeadDisruptor messageDeadDisruptor;
 
    public ConfirmMessageTask(String id, NotifyMessage message) {
        super(id, message.getNextConfirmExecuteTime());
        this.message = message;
        this.messageConfirmDisruptor = MessageConfirmDisruptor.instance();
        this.messageDeadDisruptor = MessageDeadDisruptor.instance();
    }
 
    @Override
    public void run() {
        if (!message.isGreaterThanMaxConfirmCount()) {
            messageConfirmDisruptor.getMessageProducer(ProducerName.MESSAGE_CONFIRM_PRODUCER).sendData(new RequestDto<NotifyMessage>(message));
            return;
        } else if (!message.meesageStatusIsDead()) {
            messageDeadDisruptor.getMessageProducer(ProducerName.MESSAGE_DEAD_PRODUCER).sendData(new RequestDto<NotifyMessage>(message));
            return;
        } else {
            LOGGER.info("消息放弃状态确认，消息状态已经死亡，消息序列号： " + message.getMessageSquece());
            return;
        }
    }
}